home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr51 / lib201.zip / TIME.PRG < prev    next >
Text File  |  1993-04-27  |  10KB  |  250 lines

  1. *-------------------------------------------------------------------------------
  2. *-- Program...: TIME.PRG
  3. *-- Programmer: Ken Mayer (CIS: 71333,1030)
  4. *-- Date......: 04/19/1993
  5. *-- Notes.....: These are a series of routines that deal with time strings,
  6. *--             and so on. Very useful. See README.TXT for more details on
  7. *--             the use of this library file.
  8. *-------------------------------------------------------------------------------
  9.  
  10. FUNCTION Delay
  11. *-------------------------------------------------------------------------------
  12. *-- Programmer..: Jay Parsons (CIS: 70160,340)
  13. *-- Date........: 03/01/1992
  14. *-- Notes.......: Delay Loop.  Returns .T. after lapse of given number of 
  15. *--               seconds.  Accurate to one tick, about 1/18.2 secs.
  16. *-- Written for.: dBASE IV, Version 2.0 or higher
  17. *-- Rev. History: 03/01/1992 -- Original function
  18. *--               04/20/1993 -- modified for dBASE IV 2.0 syntax, bug fixed
  19. *-- Calls.......: TIME2SEC()           Function in TIME.PRG
  20. *-- Called by...: Any
  21. *-- Usage.......: Delay(<nSeconds>)
  22. *-- Example.....: lX= Delay(10.25)
  23. *-- Returns.....: Logical
  24. *-- Parameters..: nSeconds = number of seconds to delay
  25. *-------------------------------------------------------------------------------
  26.  
  27.     parameters nSeconds         && up to 86400, one day
  28.         private nTimeout, nTimenow, lRollover
  29.         nTimeout = 100 * ( Time2Sec( time( 0 ) ) + nSeconds )
  30.         if nTimeout > 8640000
  31.           lRollover = .T.
  32.           nTimeout = nTimeout - 8640000
  33.         else
  34.           lRollover = .F.
  35.         endif
  36.         do while .T.
  37.           nTimenow = 100 * Time2Sec( time( 0 ) )
  38.           if nTimenow < nTimeout
  39.             lRollover = .F.
  40.           else
  41.             if .not. lRollover
  42.               exit
  43.             endif
  44.           endif
  45.         enddo
  46.  
  47. RETURN .T.
  48. *-- EoF: Delay()
  49.  
  50. FUNCTION Time2Sec
  51. *-------------------------------------------------------------------------------
  52. *-- Programmer..: Jay Parsons (CIS: 70160,340)
  53. *-- Date........: 03/01/1992
  54. *-- Notes.......: Convert HH:MM:SS or HH:MM:SS.SS string to seconds.
  55. *-- Written for.: dBASE IV
  56. *-- Rev. History: 03/01/1992 -- Original
  57. *-- Calls.......: None
  58. *-- Called by...: Any
  59. *-- Usage.......: Time2Sec("<cTime>")
  60. *-- Example.....: ?Time2Sec("01:24:15")
  61. *-- Returns.....: Numeric
  62. *-- Parameters..: cTime = Time string in format HH:MM:SS or HH:MM:SS.SS
  63. *-------------------------------------------------------------------------------
  64.     
  65.     parameters cTime
  66.     private cTemp, nSecs
  67.     cTemp = cTime
  68.     nSecs = 3600 * val( cTemp )
  69.     cTemp = substr( cTemp, at( ":", cTemp ) + 1 )
  70.     nSecs = nSecs + 60 * val( cTemp )
  71.     
  72. RETURN nSecs + val( substr( cTemp, at( ":", cTemp ) + 1 ) )
  73. *-- EoF: Time2Sec()
  74.  
  75. FUNCTION Sec2Time
  76. *-------------------------------------------------------------------------------
  77. *-- Programmer..: Jay Parsons (CIS: 70160,340)
  78. *-- Date........: 03/01/1992
  79. *-- Notes.......: Convert number of seconds to time string in format of
  80. *--               HH:MM:SS or HH:MM:SS.SS.
  81. *-- Written for.: dBASE IV
  82. *-- Rev. History: 03/01/1992 -- Original 
  83. *-- Calls.......: None
  84. *-- Called by...: Any
  85. *-- Usage.......: Sec2Time("<cTime>")
  86. *-- Example.....: ?Sec2Time(30001.3)
  87. *-- Returns.....: Character String
  88. *-- Parameters..: nSeconds = Seconds to be converted 
  89. *-------------------------------------------------------------------------------
  90.  
  91.     parameters nSeconds
  92.     private nHrs, nMins , nSecs, cTemp
  93.     nSecs = mod( nseconds, 86400 )
  94.     nHrs  = int( nSecs / 3600 )
  95.     nSecs = nSecs - nHrs * 3600
  96.     nMins = int( nSecs / 60 )
  97.     nSecs = nSecs - nMins * 60
  98.     cTemp = transform( nHrs, "@L 99" ) + ":" + transform( nMins, "@L 99" ) ;
  99.       + ":"
  100.  
  101. RETURN cTemp+iif(nSecs=int(nSecs),transform(nSecs,"@L 99"),;
  102.                  transform(nSecs,"@L 99.99"))
  103. *-- EoF: Sec2Time()
  104.  
  105. FUNCTION DiffTime
  106. *-------------------------------------------------------------------------------
  107. *-- Programmer..: Jay Parsons (CIS: 70160,340)
  108. *-- Date........: 03/01/1992
  109. *-- Notes.......: Calculate difference between two times given as HH:MM:SS 
  110. *--               strings. If second time is smaller than first, assumes 
  111. *--               midnight passage. Returns HH:MM:SS string, or HH:MM:SS.SS 
  112. *--               string if fractional seconds passed.
  113. *-- Written for.: dBASE IV
  114. *-- Rev. History: 03/01/1992 -- Original
  115. *-- Calls.......: TIME2SEC()           Function in TIME.PRG
  116. *--               SEC2TIME()           Function in TIME.PRG
  117. *-- Called by...: Any
  118. *-- Usage.......: DiffTime("<cTime1>","<cTime2>")
  119. *-- Example.....: ?DiffTime("2:03:24","5:12:33")
  120. *-- Returns.....: Character String
  121. *-- Parameters..: cTime1 = Time to subtract from cTime2
  122. *--               cTime2 = Time to subtract from (larger value, unless
  123. *--                            after midnite)
  124. *-------------------------------------------------------------------------------
  125.  
  126.     parameters cTime1, cTime2
  127.  
  128. RETURN Sec2Time( 86400 + Time2Sec( cTime2 ) - Time2Sec( cTime1 ) )
  129. *-- EoF: DiffTime()
  130.  
  131. FUNCTION Civ2Mil
  132. *-------------------------------------------------------------------------------
  133. *-- Programmer..: Jay Parsons (CIS: 70160,340)
  134. *-- Date........: 03/01/1992
  135. *-- Notes.......: Converts string like "12:59 a.m." to standard 24-hour 
  136. *--               HH:MM:SS. If the string contains neither "a" nor "p", the 
  137. *--               hours will not be converted.
  138. *-- Written for.: dBASE IV
  139. *-- Rev. History: 03/01/1992 -- Original 
  140. *-- Calls.......: None
  141. *-- Called by...: Any
  142. *-- Usage.......: Civ2Mil("<cCivilTime>")
  143. *-- Example.....: ?Civ2Mil("2:03:24 a.m.")
  144. *-- Returns.....: Character String
  145. *-- Parameters..: cCivilTime = Time to convert to 24 hour time.
  146. *-------------------------------------------------------------------------------
  147.  
  148.     parameters cCiviltime
  149.     private cTstring, nTime
  150.     cTstring =  trim( lower( cCiviltime ) )
  151.     nTime = val( cTstring )
  152.     do case
  153.       case "p" $ cTstring
  154.         cTstring = left( cTstring, at( "p", cTstring ) - 1 )
  155.             nTime = mod( nTime, 12 ) + 12
  156.       case "a" $ cTstring
  157.         cTstring = left( cTstring, at( "a", cTstring ) - 1 )
  158.         nTime = mod( nTime, 12 )
  159.     endcase
  160.     cTstring = transform( nTime, "@L 99" ) ;
  161.       + trim( substr( cTstring, at( ":", cTstring ) ) )
  162.     
  163. RETURN cTstring + iif( len( cTstring ) = 5, ":00", "" )
  164. *-- EoF: Civ2Mil()
  165.  
  166. FUNCTION Mil2Civ
  167. *-------------------------------------------------------------------------------
  168. *-- Programmer..: Jay Parsons (CIS: 70160,340)
  169. *-- Date........: 03/01/1992
  170. *-- Notes.......: Converts HH:MM:SS 24-hour string to a.m or p.m. notation.
  171. *-- Written for.: dBASE IV
  172. *-- Rev. History: 03/01/1992 -- Original 
  173. *-- Calls.......: None
  174. *-- Called by...: Any
  175. *-- Usage.......: Mil2Civ("<cMilTime>")
  176. *-- Example.....: ?Mil2Civ("14:03:24")
  177. *-- Returns.....: Character String
  178. *-- Parameters..: cMilTime = Time to convert to 24 hour time.
  179. *-------------------------------------------------------------------------------
  180.  
  181.     parameters cMiltime
  182.     private cCivtime, nHours, cMins
  183.     cCivtime = ltrim( trim( cMiltime ) )
  184.     nHours = val( cCivtime )
  185.     cMins = substr( cCivtime, at( ":", cCivtime ) ) + " " ;
  186.       + iif( nHours > 11, "p.m.", "a.m." )
  187.     
  188. RETURN ltrim( str( mod( nHours + 11, 12 ) + 1 ) ) + cMins
  189. *-- EoF: Mil2Civ()
  190.  
  191. FUNCTION IsAmPm
  192. *-------------------------------------------------------------------------------
  193. *-- Programmer..: Charles Miedzinksi (Borland Technical Support)
  194. *-- Date........: 11/05/1992
  195. *-- Notes.......: Taken from TechNotes (??), Checks to see if a character
  196. *--               string is in proper AM/PM time format.
  197. *-- Written for.: dBASE IV 1.1, 1.5
  198. *-- Rev. History: 11/05/1992 -- Modified a bit for the Library ...
  199. *-- Calls.......: None
  200. *-- Called by...: Any
  201. *-- Usage.......: IsAmPm(<cTime>)
  202. *-- Example.....: ?IsAmPm("20:15:05")   && should return .F.
  203. *-- Returns.....: Logical
  204. *-- Parameters..: cTime = a time string in format: "HH:MM:SS a/pm"
  205. *--                       Seconds part of string is optional, as is A/PM
  206. *-------------------------------------------------------------------------------
  207.     
  208.     parameters cTime
  209.  
  210. RETURN iif(val(left(cTime, 2)) >= 1 .and. val(left(cTime, 2)) <= 12, ;
  211.      iif(val(substr(cTime, 4, 2)) >= 0 .and. val(substr(cTime, 4, 2)) <= 59, ;
  212.      iif(substr(cTime, 6, 1) $ 'aApP', .T., ;
  213.      iif(val(substr(cTimer, 6, 2)) >= 0 .and. val(substr(cTime, 6, 2)) <= 59, ;
  214.      .T., .F.)), .F.), .F.)
  215. *-- EoF: IsAmPm()
  216.  
  217. FUNCTION AddTimes
  218. *-------------------------------------------------------------------------------
  219. *-- Programmer..: Angus Scott-Fleming (CIS: 75500,3223)
  220. *-- Date........: 05/28/1992
  221. *-- Notes.......: Add two times
  222. *-- Written for.: dBASE IV
  223. *-- Rev. History: 05/28/1992 -- Original
  224. *-- Calls.......: Time2Sec               Function in TIME.PRG
  225. *--               Sec2Time               Function in TIME.PRG
  226. *-- Called by...: Any
  227. *-- Usage.......: AddTimes("<cTime1>","<cTime2>") 
  228. *--                OR 
  229. *--               AddTimes("<cTime1>",<nHours>)
  230. *-- Example.....: ?AddTimes("01:24:15","02:00:00") 
  231. *--                    OR 
  232. *--               ?AddTimes("01:24:15",2.5)
  233. *-- Returns.....: cTime    = Time string in format HH:MM:SS or HH:MM:SS.SS
  234. *-- Parameters..: cTime1,2 = Time string in format HH:MM:SS or HH:MM:SS.SS
  235. *--               nHours   = Numeric (number of hours to add to cTime1
  236. *-------------------------------------------------------------------------------
  237.  
  238. parameters Time_One,Time_Two
  239.  
  240. if type("Time_Two") = "N"
  241.   RETURN Sec2Time(Time2Sec(Time_One)+Time_Two*3600)
  242. endif
  243.  
  244. RETURN Sec2Time(Time2Sec(Time_One)+Time2Sec(Time_Two))
  245. *-- EoF: AddTimes()
  246.  
  247. *-------------------------------------------------------------------------------
  248. *-- EoP: TIME.PRG
  249. *-------------------------------------------------------------------------------
  250.